define(function (require) {
    var $ = require('jquery');
    var lib = require('lib');
    lib = new lib();
    var lrz = require('cropit');

    function uploadImg() {
        $('#image-cropper').cropit();
        // Handle rotation
        $('.rotate-cw').click(function () {
            $('#image-cropper').cropit('rotateCW');
        });
        $('.rotate-ccw').click(function () {
            $('#image-cropper').cropit('rotateCCW');
        });

        //点击上传
        $("#exportClick").on("click", function () {
            exportClick();
        });
        function exportClick() {
            var imageData = $('#image-cropper').cropit('export', {
                type: 'image/jpeg'
            });
            if (!imageData) {
                alert("选择图片");
            } else {
                //获取上传认证token
                $.getJSON("/getToken", function (data) {
                    putb64(imageData.substring(23), data.token);
                });
            }
        }

        //点击弹出剪裁弹框
        $("#img-sel-box").on("click", function () {
            $("#image-cropper-bg").show();
        });
        //关闭弹窗
        $("#remodal-close").on("click", function () {
            $("#image-cropper-bg").hide();
        });
        //获取图片字节大小
        function fileSize(str) {
            var fileSize;
            if (str.indexOf('=') > 0) {
                var indexOf = str.indexOf('=');
                str = str.substring(0, indexOf);//把末尾的’=‘号去掉
            }

            fileSize = parseInt(str.length - (str.length / 8) * 2);
            return fileSize;
        }

        //上传base64图片
        function putb64(pic, token) {
            var url = "http://upload-z1.qiniu.com/putb64/" + fileSize(pic);
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4) {
                    var keyText = xhr.responseText;
                    keyText = JSON.parse(keyText);
                    //成功后拼接地址
                    lib.ajx(lib.getReq().ser + 'member/setMemberInfo.action', {'headUrl': keyText.key}, function (data) {
                            if (data.infocode == '0') {
                                alert('头像保存成功！');
                                location.reload();
                            } else if (data.infocode == '2') {
                                location.href = "/login.html?backUrl=" + lib.getBackUrl();
                            } else {
                                alert('服务器开小差了，请稍后再试。');
                            }
                        },
                        function () {
                            console.log('uploadImg.js => setMemberInfo.action fail!');
                        });
                    $("#image-cropper-bg").hide();
                }
            };
            xhr.open("POST", url, true);
            xhr.setRequestHeader("Content-Type", "application/octet-stream");
            xhr.setRequestHeader("Authorization", 'UpToken ' + token);
            xhr.send(pic);
        }
    }

    return uploadImg();
});
